package com.globallogic.bean;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIPanel;

import com.globallogic.consts.AppPages;
import com.globallogic.model.Md5Encoder;
import com.globallogic.model.service.LoginResult;
import com.globallogic.model.service.UserService;

@ManagedBean
@SessionScoped
public class LoginBean {

	public String login() {

		LoginResult loginResult;
        String resultPage; 
		
		loginResult = userService.login(name, password);
		loginErrorPanel.setRendered(loginResult == LoginResult.ERROR);

		if ((loginResult == LoginResult.ERROR)
				|| (loginResult == LoginResult.USER)) {
			isLoggedUserAdmin = false;
			resultPage = AppPages.dailyPlanList;
		} else {
			isLoggedUserAdmin = true;
			resultPage = AppPages.adminDashboard;
		}

		return resultPage + "?faces-redirect=true";
	}

	public String logOff() {
		name = "";
		password = "";
		isLoggedUserAdmin = false;
		
	    return AppPages.login + "?faces-redirect=true"; 	
	}
	
	public UIPanel getLoginErrorPanel() {
		return loginErrorPanel;
	}

	public void setLoginErrorPanel(UIPanel value) {
		this.loginErrorPanel = value;
	}

	public boolean getIsLoggedUserAdmin() {
		return isLoggedUserAdmin;
	}

	public String getName() {
		return name;
	}

	public void setName(String value) {
		this.name = value;
	}

	public void setPassword(String value) {
		this.password = Md5Encoder.getEncodedValue(value);
	}

	public String getPassword() {
		return password;
	}

	private UIPanel loginErrorPanel;

	@EJB
	private UserService userService;

	private String name;
	private String password;
	private boolean isLoggedUserAdmin;

}
